package com.nuotu.datawall.clouddata.layoutData;

import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.nuotu.datawall.clouddata.been.BorrowChartCount;
import com.nuotu.datawall.clouddata.been.RtInfo;
import com.nuotu.datawall.clouddata.control.Token;
import com.nuotu.datawall.clouddata.util.EchartOptionUtil;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;
import com.nuotu.datawall.clouddata.view.EchartView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class BorrowCountChartLayout {
    private static volatile BorrowCountChartLayout instance;
    private static Context mContext;
    private static EchartView echartView;
    private int tempSize=0;
    public static BorrowCountChartLayout getInstance(Context context) {
        if (instance == null) {
            synchronized (BorrowCountChartLayout.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new BorrowCountChartLayout();
                }
            }
        }
        return  instance;
    }
    public BorrowCountChartLayout init(EchartView echartView){
        this.echartView=echartView;
        echartView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshData();
            }
        });
        return instance;
    }
    Object[] x1 = new Object[]{};
    Object[] y1 = new Object[]{};
    public void refreshData(){
        RequestParams params = Tool.getRequestParams(mContext, ServerConfig.POST_BORROWCOUNT_7_HOT_URL_KEY);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                RtInfo rtInfo= JsonUtil.fromJson(result, RtInfo.class);
                BorrowChartCount borrowCount=JsonUtil.fromJson(JsonUtil.toJson(rtInfo.getData()), BorrowChartCount.class);
                x1=borrowCount.getxList();
                y1=borrowCount.getyList();
                final String title=borrowCount.getTitle();
                x.task().autoPost(new Runnable() {
                    @Override
                    public void run() {
                        echartView.refreshEchartsWithOption(EchartOptionUtil.getLineChartOptions(title,x1, y1));
                    }
                });
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Token.getInstance(mContext).refreshToken(ex.toString());
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }

}
